﻿// <copyright file="StripHtmlConverter.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace Channel9Kinect.Core
{
    #region

    using System;
    using System.Globalization;
    using System.ServiceModel.Syndication;
    using System.Text.RegularExpressions;
    using System.Windows.Data;

    #endregion

    public class StripHtmlConverter : IValueConverter
    {
        #region Constants and Fields

        private const string linebreak1 = "<br>";

        private const string linebreak2 = "<br/>";

        private static readonly Regex htmlRegex = new Regex("<.+?>", RegexOptions.Singleline | RegexOptions.Compiled);

        #endregion

        #region Public Methods

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var content = (TextSyndicationContent)value;
            string str = htmlRegex.Replace(content.Text, match =>
                {
                    string tag = match.Value.ToLower();
                    if (tag == linebreak1 || tag == linebreak2)
                    {
                        return "\r\n";
                    }
                    return string.Empty;
                });
            return str.Replace("&nbsp;", " ").Replace("&amp;", "&").Replace("&quot;", "\"").Replace("&apos;", "'");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}